#include <iostream>
using namespace std;

const int N = 50005;

int u[N], d[N];

int find(int x)
{
    if(u[x] != x) {
        int o = find(u[x]); // 找根
        d[x] += d[u[x]];    // 新距 = 旧距 + 父的新距(要先更新父的新距,因此find在上一句)
        u[x] = o;           // 更新父为根
    }
    return u[x];
}

int main()
{
    int n, k, t, x, y, ans = 0; cin >> n >> k;
    for(int i = 1; i <= n; ++i) u[i] = i;
    while(k--)
    {
        cin >> t >> x >> y;
        if(x > n || y > n) ans++;
        else if(t == 1) {
            int p1 = find(x), p2 = find(y);
            if(p1 == p2) {
                if((d[x] - d[y]) % 3) ans++;
            } else {
                u[p1] = p2;
                d[p1] = d[y] - d[x]; // d[p1] + d[x] = d[y]
            }
        } else {
            int p1 = find(x), p2 = find(y);
            if(p1 == p2){
                if((d[x] - d[y] - 1) % 3) ans++;
            } else {
                u[p1] = p2;
                d[p1] = d[y] - d[x] + 1; // d[p1] + d[x] = d[y] + 1
            }
        }
    }
    cout << ans;
    return 0;
}